package com.kingland.zmb.config;

import com.kingland.zmb.mapper.UserModelMapper;
import com.kingland.zmb.security.ResponsiveAuthenticationFailureHandler;
import com.kingland.zmb.security.ResponsiveAuthenticationSuccessHandler;
import com.kingland.zmb.service.UserDetailServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author zmb
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private final UserModelMapper userMapper;

    @Override
    protected UserDetailsService userDetailsService() {
        return new UserDetailServiceImpl(userMapper);
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        // given a default strength value
        return new BCryptPasswordEncoder(4);
    }

    public SecurityConfig(UserModelMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http
                .csrf(AbstractHttpConfigurer::disable)
                .authorizeRequests(authorize ->
                        authorize
                                .antMatchers( "/login", "/logout").permitAll()
                                .anyRequest().authenticated())
                .formLogin()
                .loginProcessingUrl("/login")   //配置表单的数据发送api
                .successHandler(new ResponsiveAuthenticationSuccessHandler())
                .failureHandler(new ResponsiveAuthenticationFailureHandler())
                .and()
                .logout().logoutUrl("/logout");

    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService()).passwordEncoder(passwordEncoder());
    }


}
